package com.bestcem.xm.award.service.mq.send.impl;

import cn.hutool.json.JSONUtil;
import com.bestcem.xm.award.mq.dto.*;
import com.bestcem.xm.award.service.mq.send.AwardMessageService;
import com.bestcem.xm.common.core.config.mq.XmMessageProducer;
import com.bestcem.xm.common.core.constant.StreamMqBinderConstants;
import com.fasterxml.jackson.core.JsonProcessingException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author leilei.zang
 */
@Slf4j
@Service
public class AwardMessageServiceImpl implements AwardMessageService {

    private XmMessageProducer messageProducer;

    @Autowired
    public void setMessageProducer(XmMessageProducer messageProducer) {
        this.messageProducer = messageProducer;
    }

    /**
     * 发送延迟开始抽奖奖励消息
     */
    @Override
    public void awardDrawStartSend(DrawAwardStartDTO drawAwardStartDTO, Date delayDate) {
        try {
            messageProducer.sendDelay(StreamMqBinderConstants.AWARD_DRAW_START_OUT, drawAwardStartDTO, delayDate);
        } catch (JsonProcessingException e) {
            log.error("[award]发送延迟开始红包奖励消息失败:{}", JSONUtil.toJsonStr(drawAwardStartDTO), e);
        }
    }

    /**
     * 发送延迟结束抽奖奖励消息
     */
    @Override
    public void awardDrawStopSend(DrawAwardStopDTO drawAwardStopDTO, Date delayDate) {
        try {
            messageProducer.sendDelay(StreamMqBinderConstants.AWARD_DRAW_STOP_OUT, drawAwardStopDTO, delayDate);
        } catch (JsonProcessingException e) {
            log.error("[award]发送延迟开始红包奖励消息失败:{}", JSONUtil.toJsonStr(drawAwardStopDTO), e);
        }
    }

    /**
     * 奖励发送清理临时表通知
     */
    @Override
    public void awardTempCleanSend(TempCleanDTO tempCleanDTO, int delayDate) {
        try {
            messageProducer.sendDelay(StreamMqBinderConstants.AWARD_TEMP_CLEAN_OUT, tempCleanDTO, delayDate);
        } catch (JsonProcessingException e) {
            log.error("[Award] 发送延迟清除此temp_id信息消息失败:{}", JSONUtil.toJsonStr(tempCleanDTO), e);
        }
    }

    /**
     * 奖励发送红包开始消息
     */
    @Override
    public void awardRedPacketStartSend(RedPacketAwardStartDTO redPacketAwardStartDTO, Date delayDate) {
        try {
            messageProducer.sendDelay(StreamMqBinderConstants.AWARD_RED_PACKET_START_OUT, redPacketAwardStartDTO, delayDate);
        } catch (JsonProcessingException e) {
            log.error("[award]发送延迟开始红包奖励消息失败:{}", JSONUtil.toJsonStr(redPacketAwardStartDTO), e);
        }
    }

    /**
     * 奖励发送红包结束消息
     */
    @Override
    public void awardRedPacketStopSend(RedPacketAwardStopDTO redPacketAwardStopDTO, Date delayDate) {
        try {
            messageProducer.sendDelay(StreamMqBinderConstants.AWARD_RED_PACKET_STOP_OUT, redPacketAwardStopDTO, delayDate);
        } catch (JsonProcessingException e) {
            log.error("[award]发送延迟结束红包奖励消息失败:{}", JSONUtil.toJsonStr(redPacketAwardStopDTO), e);
        }
    }

    /**
     * 奖励消费查询红包发放结果消息
     */
    @Override
    public void awardRedPacketSendingSend(RedPacketAwardSendingDTO redPacketAwardSendingDTO, int delayDate) {
        try {
            messageProducer.sendDelay(StreamMqBinderConstants.AWARD_RED_PACKET_SENDING_OUT, redPacketAwardSendingDTO, delayDate);
        } catch (JsonProcessingException e) {
            log.error("[award]发送检查红包发送状态mq消息失败:{}", JSONUtil.toJsonStr(redPacketAwardSendingDTO), e);
        }
    }

    /**
     * 奖励消费奖励开始消息
     */
    @Override
    public void awardAwardStartSend(AwardStartDTO awardStartDTO, Date delayDate) {
        try {
            messageProducer.sendDelay(StreamMqBinderConstants.AWARD_AWARD_START_OUT, awardStartDTO, delayDate);
        } catch (JsonProcessingException e) {
            log.error(" [award] 发送延迟开始奖励消息失败:{}", JSONUtil.toJsonStr(awardStartDTO), e);
        }
    }

    /**
     * 奖励消费奖励结束消息
     */
    @Override
    public void awardAwardStopSend(AwardStopDTO awardStopDTO, Date delayDate) {
        try {
            messageProducer.sendDelay(StreamMqBinderConstants.AWARD_AWARD_STOP_OUT, awardStopDTO, delayDate);
        } catch (JsonProcessingException e) {
            log.error(" [award] 发送延迟结束奖励消息失败:{}", JSONUtil.toJsonStr(awardStopDTO), e);
        }
    }

    /**
     * 发送中奖短信通知消息
     */
    @Override
    public void awardDrawSmsSend(DrawAwardSendSmsDTO smsDTO) {
        try {
            messageProducer.send(StreamMqBinderConstants.AWARD_DRAW_SMS_OUT, smsDTO);
        } catch (JsonProcessingException e) {
            log.error(" [award] 发送中奖短信通知消息失败:{}", JSONUtil.toJsonStr(smsDTO), e);
        }
    }

    /**
     * 项目更新-fanout
     */
    @Override
    public void qdesProjectUpdatedSend(ProjectUpdatedDTO projectUpdatedDTO) {
        try {
            log.error(" [award] qdesProjectUpdatedSend 发送消息:{}", JSONUtil.toJsonStr(projectUpdatedDTO));
            messageProducer.send(StreamMqBinderConstants.PROJECT_UPDATED, projectUpdatedDTO);
        } catch (JsonProcessingException e) {
            log.error(" [award] 项目更新发送消息失败:{}", JSONUtil.toJsonStr(projectUpdatedDTO), e);
        }
    }

    /**
     * 奖励通知-工单通知触发队列消息
     */
    @Override
    public void notificationTriggerSend(TicketNotificationTriggerDTO notificationTriggerDTO) {
        if (log.isInfoEnabled()) {
            log.info("[award] 奖励通知-工单通知触发队列消息 {} ", JSONUtil.toJsonStr(notificationTriggerDTO));
        }
        try {
            messageProducer.send(StreamMqBinderConstants.NOTIFICATION_TRIGGER_OUT, notificationTriggerDTO);
        } catch (JsonProcessingException e) {
            log.error("[award] 奖励通知-工单通知触发队列消息失败 {} ", JSONUtil.toJsonStr(notificationTriggerDTO), e);
        }
    }
}
